{% extends "kadmin/base.html" %}
{% block content_title %}
<h1>Elastic Search</h1>
{% endblock %}

{% block extrastyle %}
  {{ block.super }}
  <style type="text/css">
    div#content div {
      margin-bottom: .5em;
    }
    .disabled {
      color: #ccc;
    }
    progress {
      width: 400px;
    }
    dd {
      margin-left: 1em;
    }
    input[type="submit"].DANGER {
      border: 3px red solid;
      font: bold 12px/14px serif;
    }
    .errorspan {
      background: #ffc;
      border: 1px solid red;
      padding: 1.5px;
    }
    .errorspan img {
      transform: translate(0,-1px);
    }
    table.reindextable td.explanation {
       width: 40%;
    }
  </style>
{% endblock %}

{% block content %}
  <section>
    <p>
      Page last rendered: {{ now }} {{ settings.TIME_ZONE }}
    </p>
  </section>

  {% if error_messages %}
    <section>
      <h1>Errors</h1>
      {% for msg in error_messages %}
        <p>{{ msg }}</p>
      {% endfor %}
    </section>
  {% endif %}

  {% if outstanding_records.count > 0 %}
    <section>
      <p>
        Auto-refreshing every 30 seconds :: <a href="{{ request.path }}">Refresh page</a>
      </p>
      <script>setTimeout("window.location.reload(true);", 30000);</script>

      <h2>{{ outstanding_records.count }} outstanding records</h2>
      <table>
        <thead>
          <tr>
            <th>batch</th>
            <th>name</th>
            <th>created</th>
            <th>start</th>
            <th>end</th>
            <th>message</th>
            <th>delta</th>
          </tr>
        </thead>
        {% for record in outstanding_records %}
          <tr>
            <td>{{ record.batch_id }}</td>
            <td>{{ record.name }}</td>
            <td>{{ record.creation_time }}</td>
            <td>{{ record.start_time }}</td>
            <td>{{ record.end_time }}</td>
            <td>{{ record.message }}</td>
            <td>{{ record.delta }}</td>
          </tr>
        {% endfor %}
      </table>
    </section>
  {% endif %}

  {% if outstanding_chunks %}
    <section>
      <h1>Indexing in progress! Outstanding tasks: {{ outstanding_chunks }}</h1>
      <p>
      </p>
      <table>
        <thead>
          <tr>
            <th>message</th>
            <th>start time</th>
          </tr>
        </thead>
        <tbody>
          {% for record in outstanding_records %}
            <tr>
              <td>{{ record.text }}</td>
              <td>{{ record.starttime }}</td>
            </tr>
          {% endfor %}
        </tbody>
      </table>
      <p>
        Note: The number of records may not line up with the number of
        outstanding indexing tasks because records are created when
        the task starts.
      </p>
    </section>
  {% endif %}

  <section>
    <h1>Settings and Elasticsearch details</h1>
    <p>
      Settings at the time this page was loaded:
    </p>
    <table>
      <tr><th>ES_LIVE_INDEXING</th><td>{{ settings.ES_LIVE_INDEXING }}</td></tr>
      <tr><th>ES_INDEX_PREFIX</th><td>{{ settings.ES_INDEX_PREFIX }}</td></tr>
      <tr><th>ES_INDEXES</th><td>{{ settings.ES_INDEXES }}</td></tr>
      <tr><th>ES_WRITE_INDEXES</th><td>{{ settings.ES_WRITE_INDEXES }}</td></tr>
      <tr><th>Elasticsearch version</th><td>{{ es_deets.version.number }}</td></tr>
    </table>
  </section>

  <section>
    <h1>Index Status</h1>
    <p>
      All available indexes:
    </p>
    <table>
      <thead>
        <th>Index Name</th>
        <th>Documents</th>
        <th>Type</th>
        <th>Delete</th>
      </thead>

      <tbody>
        {% for index_name, index_count in indexes %}
          <tr>
            <td>{{ index_name }}</td>
            <td>{{ index_count }}</td>
            <td>
              {% if index_name in read_indexes and index_name in write_indexes %}
                READ/WRITE
              {% else %}
                {% if index_name in read_indexes %}
                  READ
                {% else %}
                  {% if index_name in write_indexes %}
                    WRITE
                  {% endif %}
                {% endif %}
              {% endif %}
            </td>
            {% if index_name not in read_indexes %}
              <td>
                <form method="POST">
                  {% csrf_token %}
                    <input type="hidden" name="delete_index" value="{{ index_name }}">
                    <input type="submit" value="Delete">
                </form>
              </td>
            {% else %}
              <td>Disabled</td>
            {% endif %}
          </tr>
        {% endfor %}
      </tbody>
    </table>

    <h2>Read indexes</h2>
    {% for index, stats in doctype_stats.items %}
      <h3>{{ index }}</h3>
      {% if stats == None %}
        <p>Index does not exist.</p>
      {% else %}
        <table>
          <thead>
            <tr><th>doctype</th><th>count</th></tr>
          </thead>
          <tbody>
            {% for doctype, count in stats.items %}
              <tr><td>{{ doctype }}</td><td>{{ count }}</td></tr>
            {% endfor %}
          </tbody>
        </table>
      {% endif %}
    {% endfor %}

    <h2>Write indexes</h2>
    {% if read_indexes == write_indexes %}
      <p>
        Write indexes are the same as the read indexes.
      </p>
    {% else %}
      {% for index, stats in doctype_stats.items %}
        <h3>{{ index }}</h3>
        {% if stats == None %}
          <p>Index does not exist.</p>
        {% else %}
          <table>
            <thead>
              <tr><th>doctype</th><th>count</th></tr>
            </thead>
            <tbody>
              {% for doctype, count in stats.items %}
                <tr><td>{{ doctype }}</td><td>{{ count }}</td></tr>
              {% endfor %}
            </tbody>
          </table>
        {% endif %}
      {% endfor %}
    {% endif %}
  </section>

  <section>
    <h1>Actions</h1>
    <table class="reindextable">
      <tr>
        <td colspan="2">
          <h2>REINDEX into existing index</h2>
        </td>
      </tr>
      <tr>
        <td class="explanation">
          <p>
            Reindex into the existing WRITE index. Don't do this if you've
            made mapping changes since this does not recreate the index with
            the new mappings.
          </p>
          {% if outstanding_chunks %}
            <p class="errornote">
              WARNING! There are outstanding index tasks! Don't launch another
              indexing pass unless you really know you want to.
            </p>
          {% endif %}
          {% if not settings.ES_LIVE_INDEXING %}
            <p class="errornote">
              WARNING! <tt>ES_LIVE_INDEXING</tt> is False so you can't
              reindex via the admin. Either enable <tt>ES_LIVE_INDEXING</tt>
              or use the command line <tt>./manage.py esreindex</tt>.
            </p>
          {% endif %}
        </td>
        <td>
          {% if doctype_write_stats != None %}
            <form method="POST">
              {% csrf_token %}
              {% for index, stats in doctype_write_stats.items %}
                <h3>{{ index }}</h3>
                {% for doctype, count in stats.items %}
                  <input id="check_{{ doctype }}" type="checkbox" name="check_{{ doctype }}" value="yes" checked>
                  <label for="check_{{ doctype }}">{{ doctype }}</label><br>
                {% endfor %}
              {% endfor %}
              <input type="submit" name="reindex" value="Reindex into write indexes"
              {% if not settings.ES_LIVE_INDEXING or outstanding_chunks %}disabled{% endif %}>
            </form>
          {% endif %}
        </td>
      </tr>

      <tr>
        <td colspan="2">
          <h2>DELETE existing index group's write index, recreate it and reindex</h2>
        </td>
      <tr>
        <td class="explanation">
          <p>
            This <strong>DELETES</strong> the existing WRITE index for a
            group, recreates it with the mappings, and indexes into the new
            index. You should have to do this only when the search mapping
            changes or when setting up the site for the first time.
          </p>
          {% if read_indexes == write_indexes %}
            <p class="errornote">
              WARNING! All read and write indexes are the same! Deleting and
              rebuilding the index would be really bad!
            </p>
          {% endif %}
        </td>
        <td>
          <form method="POST">
            <table>
              <tr>
                <th></th>
                <th>Group</th>
                <th>Read Index</th>
                <th>Write Index</th>
              </tr>
              {% for group, group_read_index, group_write_index in index_group_data %}
                <tr>
                  <td>
                    <input id="check_{{ group }}" type="checkbox" name="check_{{ group }}" value="yes"
                           {% if group_read_index != group_write_index %}checked{% endif %}>
                  </td>
                  <td><label for="check_{{ group }}">{{ group }}</label></td>
                  <td>{{ group_read_index }}</td>
                  <td>{{ group_write_index }}</td>
                  <td>
                    {% if group_read_index == group_write_index %}
                      <span class="errorspan">
                        <img src="/static/admin/img/icon_error.gif" />
                        This group's write index is a read index!
                      </span>
                    {% endif %}
                  </td>
                </tr>
              {% endfor %}
            </table>

            {% csrf_token %}
            <input class="DANGER" type="submit" name="recreate_index" value="DELETE selected indexes and reindex"
                   {% if not settings.ES_LIVE_INDEXING or outstanding_chunks %}disabled{% endif %}>
          </form>
        </td>
      </tr>

      <tr>
        <td colspan="2">
          <h2>RESET records and mark as failed</h2>
        </td>
      </tr>
      <tr>
        <td class="explanation">
          <p>
            This marks outstanding records as fail. This allows you to run a
            new reindexing pass.
          </p>
        </td>
        <td>
          <form method="POST">
            {% csrf_token %}
            <input type="hidden" name="reset" value="1">
            <input type="submit" name="reset" value="Mark records as failed">
          </form>
        </td>
      </tr>
    </table>
  </section>

  <section>
    <h1>Reindexing history</h1>
    <table>
      <thead>
        <tr>
          <th>batch</th>
          <th>name</th>
          <th>created</th>
          <th>start</th>
          <th>end</th>
          <th>status</th>
          <th>message</th>
          <th>delta</th>
        </tr>
      </thead>
      <tbody>
        {% for record in recent_records %}
          <tr>
            <td>{{ record.batch_id }}</td>
            <td>{{ record.name }}</td>
            <td>{{ record.creation_time }}</td>
            <td>{{ record.start_time }}</td>
            <td>{{ record.end_time }}</td>
            <td>{{ record.status }}</td>
            <td>{{ record.message }}</td>
            <td>{{ record.delta }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </section>
{% endblock %}
